package com.sanpu.kxe.business.card.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseFragment;
import com.sanpu.kxe.business.card.adapter.BankListAdapter;
import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.card.contract.BankListContract;
import com.sanpu.kxe.business.card.contract.CardConfig;
import com.sanpu.kxe.business.card.presenter.BankListPresenter;
import com.sanpu.kxe.business.card.provider.CardOpenProvider;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.jump.eventbus.RefreshBankEvent;
import com.sanpu.kxe.jump.eventbus.RefreshDefBankEvent;
import com.sanpu.kxe.module.roundview.RoundLinearLayout;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Description：银行卡列表
 * Created by 薛勇军 on 2019/4/12.
 */

public class BankListFragment extends BaseFragment implements BankListContract.View {

    @Bind(R.id.list_view)
    RecyclerView vRecyclerview;
    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;

    private TextView vAddBankTv, addBankTv;

    private LinearLayout vEmptyView;

    private RoundLinearLayout vAddBankRll, vBindCardRtv;

    private BankListAdapter myBankListAdapter;

    private BankListContract.Presenter mPresenter;

    private String chkSts = "";

    private String cardType;

    public static BankListFragment getInstance(String cardType) {
        BankListFragment fragment = new BankListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(CardConfig.INTENT_CARDTYPE, cardType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_reb_total;
    }

    @Override
    protected void initView() {
        super.initView();
        cardType = (String) getArguments().get(CardConfig.INTENT_CARDTYPE);
        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        View headView = LayoutInflater.from(getContext()).inflate(R.layout.view_mybank_list_head, null);
        vEmptyView = ButterKnife.findById(headView, R.id.emptyView);
        vBindCardRtv = ButterKnife.findById(headView, R.id.bindCardRtv);
        addBankTv = ButterKnife.findById(headView, R.id.addBankTv);
        View footerView = LayoutInflater.from(getContext()).inflate(R.layout.view_mybank_list_footer, null);
        vAddBankTv = ButterKnife.findById(footerView, R.id.addBankTv);
        vAddBankRll = ButterKnife.findById(footerView, R.id.addBankRll);

        myBankListAdapter = new BankListAdapter();
        myBankListAdapter.addHeaderView(headView);
        myBankListAdapter.addFooterView(footerView);
        vRecyclerview.setAdapter(myBankListAdapter);

        if (TextUtils.equals(CommonConfig.CREDIT_CARD_TYPE, cardType)) {
            vAddBankTv.setText("添加信用卡");
            addBankTv.setText("添加信用卡");
        } else {
            vAddBankTv.setText("更换储蓄卡");
            addBankTv.setText("添加储蓄卡");
        }

        mPresenter = new BankListPresenter(this, cardType);
        mPresenter.selectUrmBankPage();
    }

    @Override
    protected void setListener() {
        super.setListener();
        vAddBankRll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.equals(CommonConfig.CREDIT_CARD_TYPE, cardType)) {
                    CardOpenProvider.getInstance().openAddCreditNewActivity(getContext());
                } else {
                    CardOpenProvider.getInstance().openUpdateDefBankActivity(getContext(), chkSts);
                }
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.selectUrmBankPage();
            }
        });
        myBankListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                BankInfoOutput infoOutput = (BankInfoOutput) adapter.getItem(position);
                if (infoOutput != null) {
                    CardOpenProvider.getInstance()
                            .openBankCardDetailsActivity(getContext(), infoOutput, chkSts);
                }
            }
        });
        vBindCardRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CardOpenProvider.getInstance().openAddCreditNewActivity(getContext());
            }
        });
    }

    @Override
    public void showViewData(List<BankInfoOutput> outputs) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        if (outputs != null && outputs.size()>0) {
            vEmptyView.setVisibility(View.GONE);
            vAddBankRll.setVisibility(View.VISIBLE);
            myBankListAdapter.setNewData(outputs);
        } else {
            myBankListAdapter.setNewData(new ArrayList<BankInfoOutput>());
            vEmptyView.setVisibility(View.VISIBLE);
            vAddBankRll.setVisibility(View.GONE);
        }
        myBankListAdapter.notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshCheck(RefreshDefBankEvent event) {
        if (event != null) {
            chkSts = event.getChkSts();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshBank(RefreshBankEvent event) {
        if (event != null) {
            mPresenter.selectUrmBankPage();
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Override
    public void setPresenter(BankListContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
